<template>
	<view>
		<view class="content">
			<view style="margin: 100upx auto;text-align: center;">
				<image src="../../../static/logo.png" mode="widthFix"></image>
			</view>
			<view style="padding-left: 50upx;font-size: 32upx;">
				为了您更好的体验,申请获取以下权限
			</view>
			<view style="padding-left: 50upx;color: #ccc;margin-top: 40upx;font-size: 30upx;">
				获得您的公开信息(昵称,头像等)
			</view>
			<button type="" class="login-btn" open-type="getUserInfo" @getuserinfo="wxGetUserInfo">授权登录</button>
			<view class="tohome" @click="navTo('/pages/page/page')" >
				~~随便逛逛~~
			</view>
		</view>
		<!-- <view style="text-align: center;position: fixed;width: 100%;bottom: 50px;" @click="reLaunch" data-src="codeLogin/codeLogin">短信验证码登录</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname: '',
				avatar: '',
				province: '',
				city: '',
				pid:0, // 邀请码
			};
		},
		onLoad() {
			uni.getStorage({
				key: 'pid',
				success: (res) => { this.pid = res.data },
			});
		},
		methods: {
			wxGetUserInfo:function(res) {
				var o = this
				let info = res.detail.userInfo
				this.nickname = info.nickName
				this.avatar = info.avatarUrl
				this.province = info.province
				this.city = info.city
				let ress = res
				uni.login({
					provider: "weixin",
					success:function(loginRes) {
						uni.request({ // 获取后台给用户安排的个人信息
							url: 'https://bhh.ax1319.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=account.programslogin&app=1',
							method: 'POST',
							data: {
								code: loginRes.code,
								encryptedData: ress.detail.encryptedData,
								iv: ress.detail.iv,
								rawData: ress.detail.rawData,
								signature: ress.detail.signature,
								pid: o.pid
							},
							header:{
								'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
							},
							success: (detail) => { // 后台存取数据
								uni.request({
									url: 'https://bhh.ax1319.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=account.small_app_login&app=1',
									method: 'POST',
									data: {
										openid: detail.data.openid,
										nickname: o.nickname,
										avatar: o.avatar,
										province: o.province,
										city: o.city,
										pid: o.pid
									},
									header:{'Content-Type':'application/x-www-form-urlencoded'},
									success: res => {
										console.log(res)
									},
									fail: () => {},
									complete: () => {}
								});
								
								uni.setStorage({
									key: 'openid',
									data: detail.data.openid,
									success:()=> {
										setTimeout(()=> {
											uni.navigateBack()
										}, 1700);
									}
								})
							}
						});
					}
				})
			},
			navTo(url){
				uni.reLaunch({
					url
				}) 
			},
			jump(e){
				uni.navigateTo({
					url: e.currentTarget.dataset.src
				});
			},
		}
	}
</script>

<style lang="scss">
	page {background-color:#fff;}
	image {width:240upx;height:240upx;}
	button {margin:60upx 30upx 20upx;}
	.content {position:fixed;width:750upx;top:50%;margin-top:-550upx;}
	.input {border-bottom:1px solid #f0f0f0;margin:0 30upx;}
	.input image {width:50upx;height:50upx;margin-left:20upx;margin-right:10upx;margin-top:15upx;}
	.input input {line-height:80upx;font-size:28upx;height:80upx;}
	.login-btn {background-color: $uni-color-error;width: 50%;border-radius: 50upx;margin-left: 25%;color: #fff;}
	.tohome {text-align: center;color: #999;margin-top: 30upx;font-size: 28upx;}
</style>
